﻿namespace Helper
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Text.RegularExpressions;

    public static class ContentHelper
    {
        private static Collection<int> getFFId(HttpHelper _hh, int FId)
        {
            string input = "";
            string str3 = "末页";
            int num = 0;
            Collection<int> collection = new Collection<int>();
            Regex regex = new Regex(".*在线.*/home/\\?uid=\\d+\".*", RegexOptions.Multiline);
            do
            {
                str3 = _hh.Get("http://www.kaixin001.com/friend/?uid=" + FId.ToString() + "&start=" + num.ToString());
                input = input + str3;
                num += 40;
            }
            while (str3.Contains("末页") && str3.Contains("下一页"));
            foreach (Match match in regex.Matches(input))
            {
                int num2;
                string s = GetMidString(match.Value, "?uid=", "\"", out num2);
                if (!collection.Contains(int.Parse(s)))
                {
                    collection.Add(int.Parse(s));
                }
            }
            return collection;
        }

        public static int GetInteger(string str)
        {
            int num;
            if (!string.IsNullOrEmpty(str) && int.TryParse(str, out num))
            {
                return num;
            }
            return -1;
        }

        public static List<int> GetIntegerList(string str)
        {
            return GetIntegerList(str, ",");
        }

        public static List<int> GetIntegerList(string str, string del)
        {
            List<int> list = new List<int>();
            if (!string.IsNullOrEmpty(str))
            {
                foreach (string str2 in str.Split(del.ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
                {
                    int integer = GetInteger(str2);
                    if (integer != -1)
                    {
                        list.Add(integer);
                    }
                }
            }
            return list;
        }

        public static int GetMidInteger(string str, string start, string end)
        {
            int num;
            return GetMidInteger(str, start, end, out num);
        }

        public static int GetMidInteger(string str, string start, string end, out int pos)
        {
            return GetInteger(GetMidString(str, start, end, out pos));
        }

        public static string GetMidString(string str, string start, string end)
        {
            int num;
            return GetMidString(str, start, end, out num);
        }

        public static string GetMidString(string str, string start, string end, out int pos)
        {
            pos = -1;
            if ((string.IsNullOrEmpty(str) || string.IsNullOrEmpty(start)) || string.IsNullOrEmpty(end))
            {
                return null;
            }
            int index = str.IndexOf(start, StringComparison.CurrentCultureIgnoreCase);
            if (-1 == index)
            {
                return null;
            }
            index += start.Length;
            int num2 = str.IndexOf(end, index, StringComparison.CurrentCultureIgnoreCase);
            if (-1 == num2)
            {
                return null;
            }
            pos = num2;
            return str.Substring(index, num2 - index).Trim();
        }

        public static string GetVerifyCode(string content)
        {
            string str = "\"";
            string pattern = @"var\s*?g_verify\s*?=\s*?" + str + @"(.*?)\s*?" + str + ";";
            return Regex.Match(content, pattern, RegexOptions.Multiline).Result("$1").ToString().Trim();
        }

        private static void LogonAndGetFriendsId(HttpHelper _hh, int myId, Collection<int> _myFriendsList)
        {
            string str;
            int pos = 0;
            int num2 = 0;
            Regex regex = new Regex("/home/\\?uid=\\d+\".*", RegexOptions.Multiline);
            do
            {
                str = _hh.Get("http://www.kaixin001.com/friend/?uid=" + myId.ToString() + "&viewtype=&start=" + num2.ToString());
                foreach (Match match in regex.Matches(str))
                {
                    string s = GetMidString(match.Value, "uid=", "\"", out pos);
                    if (!_myFriendsList.Contains(int.Parse(s)))
                    {
                        _myFriendsList.Add(int.Parse(s));
                    }
                }
                num2 += 40;
            }
            while ((str.IndexOf("下一页") != -1) && (str.IndexOf("末页") != -1));
        }

        public static string NoHtml(string result)
        {
            if (result == null)
            {
                return string.Empty;
            }
            StringBuilder builder = new StringBuilder();
            bool flag = false;
            foreach (char ch in result)
            {
                switch (ch)
                {
                    case '<':
                        flag = true;
                        break;

                    case '>':
                        flag = false;
                        break;

                    default:
                        if (!flag)
                        {
                            if (ch == '\n')
                            {
                                builder.Append(" ");
                            }
                            else if (((ch != ' ') && (ch != '\t')) && (ch != '\r'))
                            {
                                builder.Append(ch);
                            }
                        }
                        break;
                }
            }
            return builder.ToString().Trim().Replace("&yen;", "￥").Replace("&nbsp;", "");
        }
    }
}

